<?php 

namespace App\Http\Controllers\Admin;
use DB;
use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Admin\ThrottlesLogins;
use Illuminate\Foundation\Admin\AuthenticatesAndRegistersUsers;
use Illuminate\Http\Request;
use Storage;
use App\Http\Requests;

class GoodsController extends Controller
{
    protected $goods_data=array();//商品数据
    protected $goods_id=NULL;//商品ID
    public $type=false;
    public $attr=false;
    public $goods=false;
    public function __construct(){
         $this->type = new \App\Type();
         $this->attr = new \App\Attr();
         $this->goods = new \App\Goods();
         $this->cate = new \App\Cate();
         $this->brand = new \App\Brand();
         $this->goodsattr = new \App\Goodsattr();
    }
    public function goods_add(Request $goodsImg){
         // DB::beginTransaction();
        if($_POST){
            //处理商品添加
            $filename = date('Y-m-d-H-i-s').'-'.uniqid().'.jpg';
            $photoRes = $goodsImg->file('goods_img')->move('admin/img/',$filename);
            $data=[
                'goods_name'=>isset($_POST['goods_name'])?$_POST['goods_name']:'',
                'goods_sn'=>isset($_POST['goods_sn'])?$_POST['goods_sn']:'',
                'cate_id'=>isset($_POST['cate_id'])?$_POST['cate_id']:'',
                'shop_price'=>isset($_POST['shop_price'])?$_POST['shop_price']:'',
                'goods_price'=>isset($_POST['goods_price'])?$_POST['goods_price']:'',
                'sort'=>isset($_POST['sort'])?$_POST['sort']:'',
                'brand_id'=>isset($_POST['brand_id'])?$_POST['brand_id']:'',
                'cate_id'=>isset($_POST['cate_id'])?$_POST['cate_id']:'',
                'goods_img' => '/admin/img/'.$filename,
                'goods_desc'=>isset($_POST['goods_desc'])?$_POST['goods_desc']:'',
                'goods_number'=>isset($_POST['goods_number'])?$_POST['goods_number']:'',
                'add_time'=>time(),
                'is_new'=>isset($_POST['is_new'])?$_POST['is_new']:'0',
                'is_hot'=>isset($_POST['is_hot'])?$_POST['is_hot']:'0',
                'is_best'=>isset($_POST['is_best'])?$_POST['is_best']:'0',
                'is_on_sale'=>isset($_POST['is_on_sale'])?$_POST['is_on_sale']:'0',
                'type_id'=>isset($_POST['type_id'])?$_POST['type_id']:'',
            ];
            $goodsId=$this->goods->getAddGoods($data);
            // if(!$goodsId){
            //     //商品添加不成功，则回滚
            //     DB::rollBack();
            //     return response();
            // }
            //处理商品属性入库
            $attr_value = $_POST['attr_value'];
            //var_dump($attr_value);die;
            foreach ($attr_value as $key => $value) {
                //判断是否为数组，如果是数组为规格否则为参数
                if (is_array($value)) {
                    //规格
                    foreach ($value as $k => $v) {
                        $attr_data[$key][$k]['attr_id']=$key;
                        $attr_data[$key][$k]['attr_value']=$v;
                        $attr_data[$key][$k]['goods_id']=$goodsId;
                    }
                }else{
                    $attrType[$key]['attr_id']=$key;
                    $attrType[$key]['attr_value']=$value;
                    $attrType[$key]['goods_id']=$goodsId;
                }
            }
            if(empty($attrType)){
                $cs = "";
            }else{
                foreach ($attrType as $key => $value) {
                    //添加商品属性关系表
                    $cs_attr_id[]=$this->goodsattr->insertGetId($value);
                }
                $cs=implode($cs_attr_id,'|');
            }
            // var_dump($attrType);die;
             //处理商品参数
            
            // if(!$cs_attr_id){
            //     //商品属性添加不成功回滚
            //     DB::rollBack();
            //     return response();
            // }
            
            //处理商品规格
            foreach ($attr_data as $key => $value) {
                foreach ($value as $k => $v) {
                    $goods_attr_id[$key][$k]=$this->goodsattr->insertGetId($v);
                }
            }
            // if(!$goods_attr_id){
            //     //商品规格添加不成功回滚
            //     DB::rollBack();
            //     return response();
            // }
            echo "<script>alert('添加成功');location.href='goods_list';</script>";     
        }else{
            //商品类型
            $typeList = $this->type->NgetAll();
            $prandList = $this->brand->getBrand();
            $data=$this->cate->getAll();
            $cateList = $this->cate->getLevel($data,0,"");
            return view('Admin/goods/goods_add',["typeList"=>$typeList,"brandList"=>$prandList,"cateList"=>$cateList]);
         }
   	    
    }
    /**
     * 添加相册数据
     */
    // private function addGoodsImgData(){
    //     $photo = $this->ThinkUpload('goods_photo');
    //     $goodsImgData = array();
    //     foreach($photo as $key => $imgPath){
    //         $goodsImgData[] = array(
    //             'goods_id' => $this->goods_id,
    //             'img_path' => $imgPath,
    //             'img_desc' => $this->goods_data['img_desc'][$key]
    //         );
    //     }
    //     $this->load->model('goodsImgModel');
    //     $result = $this->goodsImgModel->addAll($goodsImgData);
    //     if(!$result){
    //         $this->error('相册添加失败');
    //     }
    //     return true;
    // }
    public function getAttr($type_id){
        $attrInfo=$this->attr->typeAttr($type_id);
        return $attrInfo;
    }
      //自动生成货号
    public function getGoodsSn(){
        $sn='NB_'.rand(10000,99999);
        echo json_encode($sn);
    }

    public function goods_list(){
        $goodsList = $this->goods->getAll();
        // print_r($goodsList);
        return view('Admin/goods/goods_list',["goodsList"=>$goodsList]);
    }

    public function goods_edit(){

   	    return view('Admin/goods/goods_edit');
    }
  

    /**
     * 商品类型
     * @return [type] [description]
     */
    public function goods_type_add(){
        if($_POST){
            $arr=array(
                'type_name'=>$_POST['type_name'],
                'type_sort'=>$_POST['type_sort'],
                'type_status'=>$_POST['type_status']
            );
            $type= new \App\type();
            $res=$type->insert_do($arr);
            if($res){
                return redirect('/admin/goods_type_list');
            }else{
                echo "<script>alert('分类添加失败');location.href='goods_type_add';</script>";
            }

        }else{
            return view('Admin/goods/goods_type_add');
        }
   	    
    }

    public function goods_type_list(){
        $type= new \App\type();
        $res=$type->getAll();
   	    return view('Admin/goods/goods_type_list',['res'=>$res]);
    }

    public function goods_type_edit($id){
        $type= new \App\type();
        if($_POST){
            $type_id=$_POST['type_id'];
            $arr=array(
                'type_name'=>$_POST['type_name'],
                'type_sort'=>$_POST['type_sort']
            );
            $res=$type->updateOne($type_id,$arr);
            if($res){
                return redirect('/admin/goods_type_list');
            }else{
                echo "<script>alert('分类修改失败');location.href='goods_type_edit';</script>";
            }
        }else{
            $res=$type->getOne($id);
            return view('Admin/goods/goods_type_edit',['res'=>$res[0]]);
        }
   	    
    }
    public function goods_type_del($id){
        $type= new \App\type();
        $res=$type->getField($id);
        if($res){
            return redirect('/admin/goods_type_list');
        }else{
            echo "<script>alert('分类删除失败');location.href='goods_type_list';</script>";
        }
    }

   
}